package edu.neusoft.yiqibike.controller;

import com.baomidou.mybatisplus.plugins.Page;
import edu.neusoft.yiqibike.common.entity.Apply;
import edu.neusoft.yiqibike.common.entity.vo.Response;
import edu.neusoft.yiqibike.service.IApplyService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 申请表接口
 *
 * @author YooLin1c
 * @version 1.0.0
 * @since 2018-03-13 20:37
 */
@RestController
@RequestMapping("/applications")
public class ApplyController {
    private IApplyService applyService;

    @Autowired
    public ApplyController(IApplyService applyService) {
        this.applyService = applyService;
    }

    @GetMapping("/")
    public Response<Page<Apply>> list(Page<Apply> page) {
        return Response.success(applyService.getList(page));
    }

    @PostMapping("/")
    public Response<String> add(@RequestBody Apply apply) {
        if (Objects.isNull(apply)) {
            return Response.failed("申请数据缺失");
        }
        return applyService.add(apply) ? Response.success() : Response.failed();
    }

    @PutMapping("/")
    public Response<String> edit(@RequestBody Apply apply) {
        if (Objects.isNull(apply) || StringUtils.isEmpty(apply.getId())) {
            return Response.failed("申请数据缺失");
        }
        return applyService.edit(apply) ? Response.success() : Response.failed();
    }

    @DeleteMapping("/")
    public Response<String> delete(@RequestBody List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("请选中待删除的选项");
        }
        return applyService.deleteBatchByIds(ids) ? Response.success() : Response.failed();
    }
}
